/*
 * Copyright 2010 - Jeremie Brebec
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.jb.room.client;

import org.jb.room.client.rpc.RoomService;
import org.jb.room.client.rpc.RoomServiceAsync;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class Room implements EntryPoint {

	@Override
	public void onModuleLoad() {
		
		HorizontalPanel newGamePane = new HorizontalPanel();
		newGamePane.setHorizontalAlignment(HorizontalPanel.ALIGN_CENTER);
		newGamePane.setWidth("100%");
		newGameId = new TextBox();
		newGameId.setWidth("90%");
		newGamePane.add( newGameId );
		newGamePane.setCellWidth( newGameId, "70%");
		
		Button btn = new Button("Créer");
		newGamePane.add( btn );
		btn.setWidth("90%");
		newGamePane.setCellWidth( btn, "30%");
		
		newGameId.addKeyUpHandler( new KeyUpHandler() {
			@Override
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == 13)
					doBtnClick();
			}
		});
		
		btn.addClickHandler( new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				doBtnClick();
			}
		});
		
		RootPanel.get("newroom").add( newGamePane );
		
		roomService.getRooms(new AsyncCallback<String[]>() {
			
			@Override
			public void onSuccess(String[] result) {
				
				VerticalPanel panel = new VerticalPanel();
				
				if (result == null || result.length == 0) {
					panel.add( new Label("Aucune partie disponible...") );
				} else {
					for( final String r : result ) {
						final Anchor anchor = new Anchor( r, GWT.getHostPageBaseURL() + "coinche.html?room=" + r );
						panel.add( anchor );
					}
					
					if (Window.Location.getParameter("admin") != null) {
						
						FlowPanel deletePanel = new FlowPanel();
						for( final String r : result ) {
							final Button btn = new Button( "Supprimer " + r );
							btn.addClickHandler( new ClickHandler() {
								
								@Override
								public void onClick(ClickEvent event) {
									roomService.removeRoom( r, new AsyncCallback<Void>() {

										@Override
										public void onFailure(Throwable caught) {
											btn.setText("Echec suppression " + r);
										}

										@Override
										public void onSuccess(Void result) {
											btn.setText("OK suppression " + r);
										}
									});
								}
							});
							deletePanel.add( btn );
						}
						
						panel.add( deletePanel );
					}
				}
				
				RootPanel.get("wait").setVisible(false);
				RootPanel.get("rooms").add( panel );
			}
			
			@Override
			public void onFailure(Throwable caught) {				
				RootPanel.get().add( new Label("Erreur d'accès au service") );
			}
		});
		
	}
	
	private void doBtnClick() {
		if (!newGameId.getText().isEmpty()) {
			Window.Location.assign( GWT.getHostPageBaseURL() + "coinche.html?room=" + newGameId.getText() );
		}
	}
	
	private static final RoomServiceAsync roomService = GWT.create(RoomService.class);
	private TextBox newGameId;
}
